import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false
      }
    ]
  },
  getters: {
    total (state) {
      return state.goodList.reduce(
        (sum, item) => sum + item.price * item.num, 0)
    }
  },
  mutations: {
    updateNum (state, payload) {
      state.goodList.find((item) => item.name === payload.name).num =
        payload.num
    },
    delNum (state, payload) {
      state.goodList = state.goodList.filter(item => item.name !== payload.name)
    }
  },
  actions: {},
  modules: {}
})
